<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app"></div>
    <script src="./libs/react.js"></script>
    <script src="./libs/react-dom.js"></script>
    <script src="./libs/babel.min.js"></script>
    <script type="text/babel">
      const { useState } = React;

      function Todos() {
        const [todos, setTodos] = useState([]);
        const keyUpHandle = (e) => {
          if (e.target.value && e.keyCode === 13) {
            setTodos([
              { id: Date.now(), name: e.target.value, isEnd: false },
              ...todos,
            ]);
          }
        };
        return (
          <div>
            <h5>待办事项</h5>
            <input
              type="text"
              placeholder="请输入待办事项"
              onKeyUp={(e) => keyUpHandle(e)}
            />
            <ul>
              {todos.map((item) => (
                <li key={item.id}>
                  {item.name}
                  {item.isEnd ? (
                    "已完成"
                  ) : (
                    <button
                      onClick={() => {
                        setTodos(
                          todos.map((i) => {
                            // const newArry = { ...i };
                            if (i.id === item.id) {
                              //   newArry.isEnd = true;
                              i.isEnd = true;
                            }
                            // return newArry;
                            return i;
                          })
                        );
                      }}
                    >
                      完成
                    </button>
                  )}
                </li>
              ))}
            </ul>
          </div>
        );
      }

      function App() {
        return <Todos />;
      }
      ReactDOM.render(<App />, document.getElementById("app"));
    </script>
  </body>
</html>
